package com.hao.DaoImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.hao.Dao.productjsqDao;
import com.hao.Dao.productDao;
import com.hao.entity.product;
import com.hao.util.BaseDao;
import com.hao.Dao.userDao;

/**
 * Created by Administrator on 2018/7/28.
 */
public class productImpl implements productDao{
    public List<product> find(String enumber) {
        List<product> list=new ArrayList<product>();
        String sql="SELECT * FROM  equipment WHERE enumber like ? ";

        Connection conn=null;
        PreparedStatement state=null;
        ResultSet rs=null;
        try {
            conn=BaseDao.getConnection();
            state=conn.prepareStatement(sql);
                state.setString(1,"%"+enumber+"%");
            rs=state.executeQuery();

            userDao dao=new userImpl();
            productjsqDao dao1=new productjsqImpl();

            while(rs.next()){
            product pp=new product();
            pp.setEid(rs.getInt("eid"));
            pp.setEnumber(rs.getString("enumber"));
            pp.setManufactureTime(rs.getString("manufactureTime"));
            pp.setUserid(rs.getInt("userid"));
            pp.setWpid(rs.getInt("wpid"));
            pp.setInstall(rs.getInt("install"));
            pp.setAllocationDate(rs.getString("allocationDate"));
            pp.setUlist(dao.finduser(pp.getUserid()));
            pp.setPlist(dao1.finduser(rs.getInt("wpid")));
            list.add(pp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            BaseDao.closeConnection(conn, state, rs);
        }
        return list;
    }

    @Override
    public int dele(int id) {
        String sql="DELETE FROM equipment WHERE eid=?";
        Object[] ob={
                id
        };
        return BaseDao.Update(sql, ob);
    }

    @Override
    public product updfind(int id) {
        String sql="SELECT *FROM  equipment WHERE eid=? ";
        product ss=new product();
        Connection conn=null;
        PreparedStatement state=null;
        ResultSet rs=null;
        try {
            conn=BaseDao.getConnection();
            state=conn.prepareStatement(sql);
            state.setInt(1,id);
            rs=state.executeQuery();
            while(rs.next()){

                ss.setEid(rs.getInt("eid"));
                ss.setEnumber(rs.getString("enumber"));
                ss.setManufactureTime(rs.getString("manufactureTime"));
                ss.setUserid(rs.getInt("userid"));
                ss.setWpid(rs.getInt("wpid"));
                ss.setInstall(rs.getInt("install"));
                ss.setAllocationDate(rs.getString("allocationDate"));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            BaseDao.closeConnection(conn, state, rs);
        }
        return ss;
    }

    @Override
    public int upd(product upd) {
        String sql="UPDATE equipment set enumber=?,manufactureTime=?,\n" +
                "userid=?,wpid=?,`install`=?,allocationDate=? where eid=?";

        Object[] ob={
                upd.getEnumber(),
                upd.getManufactureTime(),
                upd.getUserid(),
                upd.getWpid(),
                upd.getInstall(),
                upd.getAllocationDate(),
                upd.getEid()
        };
        return BaseDao.Update(sql, ob);
    }

    @Override
    public int add(product add) {
        String sql="INSERT into equipment VALUES(null,?,?,?,?,?,?)";
        Object[] ob={
                add.getEnumber(),
                add.getManufactureTime(),
                add.getUserid(),
                add.getWpid(),
                add.getInstall(),
                add.getAllocationDate()
        };
        return BaseDao.Update(sql, ob);
    }
}
